
cap cd "C:\Users\ejm5\Dropbox\PAPI conjoint\Replication"
cap cd "/Users/paulschuler/Dropbox/projects/PAPI conjoint\Replication"

set more off

/*Create Form A*/
#delimit;
use "RawData\papi2017.dta", clear;
gen villageid = thon;
destring villageid, replace;
format villageid %20.0f;
sort villageid;


merge villageid using "RawData\PAPI2017_svyweights_alt.dta";


cap cd "C:\Users\ejm5\Dropbox\PAPI conjoint\Replication";
cap cd "/Users/paulschuler/Dropbox/projects/PAPI conjoint\Replication";
 
svyset PSW_PSU [pweight=PSW_psweight], fpc(PSW_FPC1) strata(PSW_STRATA) singleunit(certainty) ///
 || PSW_SSU, fpc(PSW_FPC2) strata(PSW_STRATA2) || villageid, fpc(PSW_FPC3) strata(PSW_STRATA3) ///
 || _n,  poststrata(tinh)  postweight(PSW_province_population);
 
 generate respondent_id=_n;
 
 /*New Missing Value Code*/
 
mvdecode idcode0 tinh tentinh huyen tenhuyen tenxa tenthon khuvuc a00* a016* a011* d305* d101d* d101b* d102_a* d407 d102_1_a*  d102  d102_1 PSW* villageid respondent_id d306*, mv(111=.a\888=.b\999=.c);

save papi2017_wghts.dta, replace;
 


keep idcode0 tinh tentinh huyen tenhuyen tenxa tenthon khuvuc a00* a016* a011* d305* d101d* d101b* d102_a* d407 d102_1_a*  d102  d102_1 PSW* villageid respondent_id d306*;
generate year=2017;
generate form=1;
rename d102_a_1 age;
rename d102_a_1_opt1 age_39;
rename d102_a_1_opt2 age_59;
rename d102_a_2 sex;
rename d102_a_2_opt1 male;
rename d102_a_2_opt2 female;
rename d102_a_3 home_province;
rename d102_a_4 education;
rename d102_a_4_opt1 high_school;
rename d102_a_4_opt2 bachelors;
rename d102_a_4_opt3 masters;
rename d102_a_4_opt4 doctorate;
rename d102_a_5 nomination;
rename d102_a_5_opt1 self_nominee;
rename d102_a_5_opt2 gov_nominee;
rename d102_a_6 party_status;
rename d102_a_6_opt1 party;
rename d102_a_6_opt2 non_party;
rename d102_a_7  job;
rename d102_a_7_opt1 VFF; 
rename d102_a_7_opt2 business;
rename d102_a_7_opt3 doctor;
rename d102_a_7_opt4 lawyer;
rename d102_a_8_opt1 trade;
rename d102_a_8_opt2 poverty;
rename d102_a_8_opt3 growth;
rename d102_a_8_opt4 environment;
rename d102_a_8_opt5 no_policy;
rename d102_a_8 policy_priority;
rename d102 vote;
replace vote=1 if vote==1;
replace vote=0 if vote==2;

rename d102_1_a_1 age_village;
rename d102_1_a_1_opt1 age_39_village;
rename d102_1_a_1_opt2 age_59_village;
rename d102_1_a_2 sex_village;
rename d102_1_a_2_opt1 male_village;
rename d102_1_a_2_opt2 female_village;
rename d102_1_a_3 education_village;
rename d102_1_a_3_opt1 high_school_village;
rename d102_1_a_3_opt2 bachelors_village;
rename d102_1_a_3_opt3 masters_village;
rename d102_1_a_3_opt4 doctorate_village;
rename d102_1_a_4 party_status_village;
rename d102_1_a_4_opt1 party_village;
rename d102_1_a_4_opt2 non_party_village;
rename d102_1_a_5  job_village;
rename d102_1_a_5_opt1 VFF_village; 
rename d102_1_a_5_opt2 business_village;
rename d102_1_a_5_opt3 doctor_village;
rename d102_1_a_5_opt4 lawyer_village;
rename d102_1 vote_village;
replace vote_village=1 if vote_village==1;
replace vote_village=0 if vote_village==2;



save NA_formA.dta, replace;

/*Create Form B*/
#delimit;
use papi2017_wghts.dta, clear;
keep idcode0 tinh tentinh huyen tenhuyen tenxa tenthon khuvuc a00* a016* a011* d305* d101d* d101b* d102_b* d407 d102 d102_1_b* d102 d102_1 PSW* villageid respondent_id;
generate year=2017;*
rename d102_b_1 age;
rename d102_b_1_opt1 age_39;
rename d102_b_1_opt2 age_59;
rename d102_b_2 sex;
rename d102_b_2_opt1 male;
rename d102_b_2_opt2 female;
rename d102_b_3 home_province;
rename d102_b_4 education;
rename d102_b_4_opt1 high_school;
rename d102_b_4_opt2 bachelors;
rename d102_b_4_opt3 masters;
rename d102_b_4_opt4 doctorate;
rename d102_b_5 nomination;
rename d102_b_5_opt1 self_nominee;
rename d102_b_5_opt2 gov_nominee;
rename d102_b_6 party_status;
rename d102_b_6_opt1 party;
rename d102_b_6_opt2 non_party;
rename d102_b_7  job;
rename d102_b_7_opt1 VFF; 
rename d102_b_7_opt2 business;
rename d102_b_7_opt3 doctor;
rename d102_b_7_opt4 lawyer;
rename d102_b_8_opt1 trade;
rename d102_b_8_opt2 poverty;
rename d102_b_8_opt3 growth;
rename d102_b_8_opt4 environment;
rename d102_b_8_opt5 no_policy;
rename d102_b_8 policy_priority;
rename d102 vote;
replace vote=0 if vote==1;
replace vote=1 if vote==2;
replace vote=. if vote>2;


rename d102_1_b_1 age_village;
rename d102_1_b_1_opt1 age_39_village;
rename d102_1_b_1_opt2 age_59_village;
rename d102_1_b_2 sex_village;
rename d102_1_b_2_opt1 male_village;
rename d102_1_b_2_opt2 female_village;
rename d102_1_b_3 education_village;
rename d102_1_b_3_opt1 high_school_village;
rename d102_1_b_3_opt2 bachelors_village;
rename d102_1_b_3_opt3 masters_village;
rename d102_1_b_3_opt4 doctorate_village;
rename d102_1_b_4 party_status_village;
rename d102_1_b_4_opt1 party_village;
rename d102_1_b_4_opt2 non_party_village;
rename d102_1_b_5  job_village;
rename d102_1_b_5_opt1 VFF_village; 
rename d102_1_b_5_opt2 business_village;
rename d102_1_b_5_opt3 doctor_village;
rename d102_1_b_5_opt4 lawyer_village;
rename d102_1 vote_village;

/*Old Code
replace vote_village=1 if vote_village==1;
replace vote_village=0 if vote_village==2;*/


/*July 18, 2019*/
replace vote_village=0 if vote_village==1;
replace vote_village=1 if vote_village==2;



generate form=2;
save NA_formB.dta, replace;

/*Append Together*/
use NA_formA.dta, clear;
append using NA_formB.dta;
gen home=1 if home_province==tinh;
replace home=0 if home_province !=tinh;


xtset respondent_id form;
generate policy_same=1 if trade==1 & l.trade==1;
replace policy_same=1 if trade==1 & f.trade==1;
replace policy_same=1 if poverty==1 & l.poverty==1;
replace policy_same=1 if poverty==1 & f.poverty==1;
replace policy_same=1 if growth==1 & l.growth==1;
replace policy_same=1 if growth==1 & f.growth==1;
replace policy_same=1 if environment==1 & l.environment==1;
replace policy_same=1 if environment==1 & f.environment==1;
replace policy_same=1 if no_policy==1 & l.no_policy==1;
replace policy_same=1 if no_policy==1 & f.no_policy==1;
replace policy_same=0 if policy_same==.;
gen no_policy2 = 0;
replace no_policy2=1 if no_policy==1 & policy_same==1;

save PAPI_Conjoint_2017.dta, replace;
lab var policy_same "Candidates Share Policy Priority";

/*************************************ISSUE*********************/
generate issue_environment=0;
replace issue_environment=3 if regexm(d306a, "moi truong");
replace issue_environment=2 if regexm(d306b, "moi truong");
replace issue_environment=1 if regexm(d306c, "moi truong");

generate issue_poverty=0;
replace issue_poverty=3 if regexm(d306a, "ngheo");
replace issue_poverty=2 if regexm(d306b, "ngheo");
replace issue_poverty=1 if regexm(d306c, "ngheo");


generate issue_growth=0;
replace issue_growth=1 if regexm(d306a, "phat trien")|regexm(d306a, "kinh te");
replace issue_growth=2 if regexm(d306b, "phat trien")|regexm(d306b, "kinh te");
replace issue_growth=3 if regexm(d306c, "phat trien")|regexm(d306c, "kinh te");

generate issue_trade=0;
replace issue_trade=1 if regexm(d306a, "dau tu")|regexm(d306a, "thuong mai");
replace issue_trade=2 if regexm(d306b, "dau tu")|regexm(d306b, "thuong mai");
replace issue_trade=3 if regexm(d306c, "dau tu")|regexm(d306c, "thuong mai");

generate issue_eastsea = 0 ;
replace issue_eastsea=1 if d306a2==41|  d306a2==43 ;
replace issue_eastsea=2 if d306b2==41| d306b2==43 ;
replace issue_eastsea=3 if d306c2==41| d306c2==43 ;

gen party_member = 0;
replace party_member = 1 if a016_opt1==1;

gen civil_society=1;
replace civil_society= 0 if a016_opt21==1;

gen respondent_age = a002;
gen respondent_male = 1;
replace respondent_male = 0 if a001==2;

sum issue*;


#delimit;
generate match=0;
replace match=1 if environment==1 & issue_environment>0;
replace match=1 if poverty==1 & issue_poverty>0;
replace match=1 if growth==1 & issue_growth>0;
replace match=1 if trade==1 & issue_trade>0;
replace match=1 if no_policy==1 & no_policy>0;
lab var match "Candidate and Respondent Share Policy Priority";

gen no_false = .;
replace no_false=1 if d101d5a==1;
replace no_false=1 if d101d5b==1;
replace no_false=0 if d101d5a==0;
replace no_false=0 if d101d5b==0;


save PAPI_Conjoint_2017.dta, replace;

/*Append 2016 and 2017 Data Together*/
use PAPI_Conjoint_2016.dta, replace;
append using  PAPI_Conjoint_2017.dta, force;
save PAPI_Conjoint_2years.dta, replace;








